package org.weed.iremote.service;

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import android.widget.Toast;

public class RemoteServiceImpl {
	private String ip;
	private int port;
	private Socket client;
	private PrintWriter writer;
	
	public RemoteServiceImpl(String ip, int port) {
		this.ip = ip;
		this.port = port;
	}
	
	public boolean connect() {
        try {
        	client = new Socket(ip, port);
        	writer = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
        	return true;   
        } catch (Exception e) {
        	e.printStackTrace();
        	client = null;
        	writer = null;
        	return false;        	
        }
	}
	
	public void disconnect() {
		try {
			client.close();
			client = null;
			writer = null;
		} catch (Exception e) {
			
		}
	}
	
	public void channel(String channel) {
		sendMessage("K" + channel + "/");
	}
	
	public void up() {
		sendMessage("K+");
	}
	
	public void down() {
		sendMessage("K-");
	}
	private void sendMessage(String message) {
		try {
			writer.println(message);
			writer.flush();
		} catch (Exception e) {
			
		}
	}
	
}
